package com.example.zhihuhelpapp.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhihuhelpapp.R;
import com.example.zhihuhelpapp.adapter.QuestionListAdapter;
import com.example.zhihuhelpapp.base.MyBaseRxActivity;
import com.example.zhihuhelpapp.bean.QuestionBean;
import com.example.zhihuhelpapp.bean.QuestionResultBean;
import com.example.zhihuhelpapp.event.UIEvent;
import com.example.zhihuhelpapp.mvp.QuestionListPresenter;
import com.example.zhihuhelpapp.mvp.contract.IQuestionListContract;
import com.example.zhihuhelpapp.utils.CoreConstant;
import com.example.zhihuhelpapp.utils.DbUtils;
import com.example.zhihuhelpapp.utils.DialogUtils;
import com.example.zhihuhelpapp.utils.SpUtils;
import com.example.zhihuhelpapp.utils.UIUtils;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 问题列表页面
 */
public class QuestionListActivity extends MyBaseRxActivity<QuestionListPresenter>
        implements IQuestionListContract.IView, MultiItemTypeAdapter.OnItemClickListener {
    private List<QuestionBean> mDatas;
    private QuestionListAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_question_list;
    }

    @Override
    protected void initView() {
        showToolBar(true,getString(R.string.go_question_list));

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mDatas = new ArrayList<>();
        mAdapter = new QuestionListAdapter(this, mDatas);
        recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        getLocalQuestionList();
    }

    @Override
    protected QuestionListPresenter createPresenter() {
        return new QuestionListPresenter();
    }

    public void getAllRaking(View view) {
        if (mDatas.size() > 0) {
            mAdapter.setGetItemRaking(true);
        } else {
            UIUtils.showToast(R.string.no_data);
        }
    }

    @Override
    public void onSaveQuestionSuccess() {

    }

    @Override
    public void onDelQuestionSuccess() {

    }

    @Override
    public void onGetLocalQuestionList(List<QuestionBean> questionBeanList) {
        mDatas.clear();
        if (questionBeanList != null && questionBeanList.size() > 0) {
            mDatas.addAll(questionBeanList);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetQuestionDetail(QuestionResultBean questionResultBean) {

    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
        // 到详情页面
        String nickName = SpUtils.getInstance().get(CoreConstant.SpConstant.KEY_NICK_NAME, "");
        if (TextUtils.isEmpty(nickName)) {
            DialogUtils.showDialog(this, "提示：", getString(R.string.tip_please_input_your_nickname_first),
                    (dialog, which) -> startActivity(new Intent(QuestionListActivity.this, SetNickNameActivity.class)));
        } else {
            QuestionDetailActivity.showQuestionDetail(this, mDatas.get(i).getQuestionId());
        }
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
        showLongPressDialog(i);
        return true;
    }

    private void showLongPressDialog(final int itemPosition) {
        final QuestionBean questionBean = mDatas.get(itemPosition);
        String[] itemList = new String[3];
        itemList[0] = "删除";
        itemList[1] = "显示排名";
        if (questionBean.isTop()) {
            itemList[2] = "取消置顶";
        }else{
            itemList[2] = "置顶";
        }
        DialogUtils.showItemListDialog(this, itemList, (dialog, position1) -> {
            dialog.dismiss();
            switch (position1) {
                case 0:
                    // 删除
                    showDelConfirmDialog(itemPosition);
                    break;
                case 1:
                    // 显示排名
                    if (questionBean.getShowRaking()==1) {
                        showError("已显示");
                        return;
                    }else{
                        questionBean.setShowRaking(1);
                        mAdapter.notifyItemChanged(itemPosition);
                    }
                    break;
                case 2:
                    // 置顶、取消置顶
                    questionBean.setTop(questionBean.isTop()?0:1);
                    DbUtils.saveQuestion(questionBean);
                    getLocalQuestionList();
                    break;
            }
        });
    }

    private void showDelConfirmDialog(int itemPosition){
        DialogUtils.showDialog(this, "提示", "确认删除？", (dialog, position) -> {
            DbUtils.delQuestion(mDatas.get(itemPosition).getQuestionId());
            getLocalQuestionList();
        });
    }

    @Override
    public void onUiEvent(UIEvent uiEvent) {
        super.onUiEvent(uiEvent);
        switch (uiEvent.getEvent()) {
            case UIEvent.EVENT_UPDATE_QUESTION_LIST:
                getLocalQuestionList();
                break;
        }
    }

    private void getLocalQuestionList() {
        mPresenter.getLocalQuestionList();
    }
}
